﻿using System;
using System.Linq;
using System.Web;
using BL.Abstract;
using BL.Generic;
using DAL.Abstract;
using DAL.Class;
using DAL.Repositories;
using DAL.Strategies;
using Model;

namespace BL.Class
{
    public class MbewBL : IMbewBL
    {
        private readonly UnitOfWork unitOfWork = new UnitOfWork();
        private readonly IFileLoadStrategy fileLoadStrategy;
        private readonly IMasterFileBL masterFileBl;

        public MbewBL()
        {
            fileLoadStrategy = new FileLoadStrategy();
            masterFileBl = new MasterFileBL();
        }

        public GenericResult<MBEW> GetAllMbewRecordsByInventoryId(int inventoryId)
        {
            try
            {
                return new GenericResult<MBEW>
                {
                    ObjectAnswer = unitOfWork.MbewRepository.Get(i => i.INVENTORYID == inventoryId),
                    BlAnswer = Answer.Success
                };
            }
            catch (Exception ex)
            {
                return new GenericResult<MBEW>
                {
                    Exception = ex.Message,
                    ObjectAnswer = null,
                    BlAnswer = Answer.Error
                };
            }
        }

        public GenericResult<MBEW> GetMbewRecordById(int mbewId)
        {
            try
            {
                return new GenericResult<MBEW>
                {
                    SingleObjectAnswer = unitOfWork.MbewRepository
                        .Get(i => i.MBEWID == mbewId)
                        .FirstOrDefault(),
                    BlAnswer = Answer.Success
                };
            }
            catch (Exception ex)
            {
                return new GenericResult<MBEW>
                {
                    Exception = ex.Message,
                    ObjectAnswer = null,
                    BlAnswer = Answer.Error
                };
            }
        }

        public GenericResult<MBEW> GetMbewRecordByPartNumber(string partnumber)
        {
            try
            {
                return new GenericResult<MBEW>
                {
                    SingleObjectAnswer = unitOfWork.MbewRepository
                        .Get(i => i.PARTNUMBER == partnumber)
                        .FirstOrDefault(),
                    BlAnswer = Answer.Success
                };
            }
            catch (Exception ex)
            {
                return new GenericResult<MBEW>
                {
                    Exception = ex.Message,
                    ObjectAnswer = null,
                    BlAnswer = Answer.Error
                };
            }
        }

        public GenericResult<int> SaveFileToServerAndProcess(string serverFilePath, int inventoryId, string separator)
        {
            try
            {
                var loadedId = masterFileBl.SaveNewMasterFile(serverFilePath, inventoryId);
                try
                {
                    var loadedRows = fileLoadStrategy.Execute(LoadFileAction.MBEW, serverFilePath, separator);
                    if (masterFileBl.UpdateLoadedFile(true, loadedId, loadedRows, "Plik załadowano"))
                    {
                        return new GenericResult<int>
                        {
                            SingleObjectAnswer = loadedRows,
                            BlAnswer = Answer.Success
                        };
                    }
                }
                catch (Exception ex)
                {
                    masterFileBl.UpdateLoadedFile(false, 0, loadedId, "Błąd ładowania");
                    return new GenericResult<int>
                    {
                        Exception = ex.Message,
                        BlAnswer = Answer.Error
                    };
                }
                return new GenericResult<int>()
                {
                    Exception = "Blad ladowania zapisu rekordu pliku do bazy",
                    BlAnswer = Answer.Error
                };
            }
            catch (Exception ex)
            {
                return new GenericResult<int>
                {
                    Exception = ex.Message,
                    SingleObjectAnswer = false,
                    BlAnswer = Answer.Error
                };
            }
        }        
    }
}
